package cn.kgc.tongxunlu.controller;

import cn.kgc.tongxunlu.pojo.Contact;
import cn.kgc.tongxunlu.pojo.ResultEntity;
import cn.kgc.tongxunlu.service.ContactService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-28 9:21
 */
@RestController
public class ContactController {

    @Resource
    ContactService contactService;

    @RequestMapping("/pageInfo")
    public ResultEntity<PageInfo<Contact>> pageInfo(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "3") Integer pageSize,
            @RequestParam(value = "cname",required = false,defaultValue = "") String cname
    ){
        PageInfo<Contact> pageInfo = contactService.pageInfo(pageNum, pageSize, cname);
        return ResultEntity.successWithoutData(pageInfo);
    }
    //根据id查询
    @RequestMapping("/selectById")
    public ResultEntity<Contact> selecyById(Integer id){
        Contact contact = contactService.selectById(id);
        return ResultEntity.successWithoutData(contact);
    }

    //增加 修改
    @RequestMapping("/add")
    public  ResultEntity<String> add(@RequestBody Contact contact){
        try {
            Integer integer=0;
            if(contact.getId()==null){
                contactService.add(contact);
            }else{
                contactService.update(contact);
            }
            return ResultEntity.successWithoutData(integer+"");
        } catch (Exception e) {
        return ResultEntity.failed(e.getMessage());
        }
    }
}
